#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 29 11:05:43 2019

@author: wuhuan
"""

# 数据读取及基本处理
import pandas as pd
import numpy as np

# plotting
import seaborn as sn
import matplotlib.pyplot as plt
#matplotlib inline

# setting params
params = {'legend.fontsize': 'x-large',
          'figure.figsize': (30, 10),
          'axes.labelsize': 'x-large',
          'axes.titlesize':'x-large',
          'xtick.labelsize':'x-large',
          'ytick.labelsize':'x-large'}

sn.set_style('whitegrid')
sn.set_context('talk')

plt.rcParams.update(params)
pd.options.display.max_colwidth = 600

# pandas display data frames as tables
from IPython.display import display, HTML

# 读入数据
train = pd.read_csv("/Users/wuhuan/Desktop/day.csv")
train.head()
print("train : " + str(train.shape))

train.info()

#对数据值型特征，用常用统计量观察其分布
train.describe()

#对类别型特征，观察其取值范围及直方图
categorical_features = ['season','mnth','weathersit','weekday']
for col in categorical_features:
    print ('\n%s属性的不同取值和出现的次数'%col)
    print (train[col].value_counts())
    train[col] = train[col].astype('object')
    
#对数值型特征，直方图
numerical_features = ['temp','atemp','hum','windspeed']
train[numerical_features].hist();

#violinplot中用x表示类别（年）信息
sn.violinplot(x="yr",y="cnt",data=train[['yr','cnt']]);

#用颜色参数hue表示类别（年）信息
import datetime

train['date'] = pd.to_datetime(train['dteday'])
train['dayofyear'] = train["date"].dt.dayofyear  #减今年的第几天

fig,ax = plt.subplots()
sn.pointplot(data=train[['dayofyear','cnt','yr']],x='dayofyear',y='cnt',
             hue='yr',ax=ax)
ax.set(title="dayly distribution of counts")

#violinplot得到详细分布
sn.violinplot(data=train[['season','cnt']],x="season",y="cnt");

#每个季节骑行量的分布不同 barplot利用矩阵条的高度反映数值变量的集中趋势
fig,ax = plt.subplots()
sn.barplot(data=train[['season',
                       'cnt']],
           x="season",y="cnt")
ax.set(title="Seasonly distribution of counts")

#3.4 月份与骑车数量的关系
fig,ax = plt.subplots()
sn.barplot(data=train[['mnth','cnt']],x="mnth",y="cnt")
ax.set(title="Monthly distribution of counts")

#3.5 天气和骑车数目的关系
fig,ax = plt.subplots()
sn.barplot(data=train[['weathersit',
                       'cnt']],
           x="weathersit",y="cnt")
ax.set(title="weathersit distribution of counts")

#3.6 工作日和节假日的分布
fig,(ax1,ax2) = plt.subplots(ncols=2)
sn.barplot(data=train,x='holiday',y='cnt',ax=ax1)
sn.barplot(data=train,x='workingday',y='cnt',ax=ax2)

#3.7 数值型特征和y之间的相关性
corrMatt = train[["temp","atemp","hum","windspeed","casual","registered",
                  "cnt"]].corr()
mask = np.array(corrMatt)
mask[np.tril_indices_from(mask)] = False
sn.heatmap(corrMatt, mask=mask,vmax=.8, square=True,annot=True)

#特征工程
#对类别型特征，观察其取值范围及直方图
categorical_features = ['season','mnth','weathersit','weekday']

#数据类型变为object，才能被get_dummies处理
for col in categorical_features:
    train[col] = train[col].astype('object')
    
X_train_cat = train[categorical_features]
X_train_cat = pd.get_dummies(X_train_cat)
X_train_cat.head()

#数值型变量预处理，
#感觉数据已经做过处理（取值都在0-1之间），这里用MinMaxScaler再处理一次
from sklearn.preprocessing import MinMaxScaler
mn_X = MinMaxScaler()
numerical_features = ['temp','atemp','hum','windspeed']
temp = mn_X.fit_transform(train[numerical_features])

X_train_num = pd.DataFrame(data=temp, columns=numerical_features, index =train.index)
X_train_num.head()

# Join categorical and numerical features
X_train = pd.concat([X_train_cat, X_train_num, train['holiday'],  train['workingday']], axis = 1, ignore_index=False)
X_train.head()

FE_train = pd.concat([train['instant'], X_train,  train['yr'],train['cnt']], axis = 1)
FE_train.to_csv('FE_day.csv', index=False)
FE_train.head()
FE_train.info()




















